package com.sdl.timesblogmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdl.timesblogmp.exception.CustomException;
import com.sdl.timesblogmp.model.entity.Tag;
import com.sdl.timesblogmp.mapper.TagMapper;
import com.sdl.timesblogmp.model.vo.PageParam;
import com.sdl.timesblogmp.service.TagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sdl
 * @since 2020-05-11
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Resource
    private TagMapper tagMapper;
    @Override
    public IPage<Tag> findTagInfo(PageParam pageParam) {
        if (pageParam.getPageNo()==0||pageParam.getPageSize()==0){
            pageParam.setPageNo(1);
            pageParam.setPageSize(6);
        }
        QueryWrapper<Tag> wrapper = new QueryWrapper();
        if (pageParam.getName()!=null){
            wrapper.like("name",pageParam.getName());
        }
        Page<Tag> page = new Page<>(pageParam.getPageNo(),pageParam.getPageSize());
        page = tagMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    public int delTag(Integer id) {
        Tag tag = tagMapper.selectById(id);
        if (null!=tag){
            int check = tagMapper.checkTagDel(id);
            if (check!=0){
                throw new CustomException("标签包含关联数据，不能删除！");
            }
            return tagMapper.deleteById(id);
        }else {
            throw new CustomException("标签不存在！");
        }
    }

    @Override
    public Tag addTag(Tag tag) {
        tagMapper.insert(tag);
        return tag;
    }
}
